//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
using CommunityServer.Galleries;
using System.Collections.Specialized;

namespace CommunityServer.Galleries.Providers
{
	public class GalleryBrowseableList : BrowseableList
	{
		protected override ApplicationType ApplicationType
		{
			get { return ApplicationType.Gallery; }
		}

		#region IBrowseableList Members

  
		public override string Name
		{
			get
			{
				return ResourceManager.GetString("photos");
			}
		}

		public override System.Collections.ArrayList Containers
		{
			get
			{
				ArrayList containers = new ArrayList();
				
				ArrayList groups = Galleries.GetGalleryGroups(true, false, false);
				foreach (Group group in groups) 
				{
					containers.Add(new GalleryBrowseableContainer(group, null, null, ""));
				}

				return containers;
			}
		}

		public override object GetNode(string listKey)
		{
			if (listKey == "")
				return this;

			int id, id2;
			PostCategory pc;
			Gallery s;
			GalleryPost p;
			Group g;
			string[] ids;
			string parentCategories;
			switch (listKey.Substring(0, 1))
			{
				case "C":
					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					id2 = int.Parse(ids[1].Substring(1));
					parentCategories = ids[2].Substring(1);

					s = Galleries.GetGallery(id2);
					pc = PostCategories.GetCategory(id, s.SectionID);
					g = Galleries.GetGalleryGroup(s.GroupID, true, false);

					return new GalleryBrowseableContainer(g, s, pc, parentCategories);

				case "S":
					id = int.Parse(listKey.Substring(1));
					s = Galleries.GetGallery(id);
					g = Galleries.GetGalleryGroup(s.GroupID, true, false);

					return new GalleryBrowseableContainer(g, s, null, "");

				case "G":

					id = int.Parse(listKey.Substring(1));
					g = Galleries.GetGalleryGroup(id, true, false);

					return new GalleryBrowseableContainer(g, null, null, "");

				case "P":

					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					parentCategories = ids[1].Substring(1);
					p = GalleryPosts.GetPicture(id);

					return new GalleryBrowseableItem(p, parentCategories);

				default:
					return null;
			}
		}

		public override StringCollection GetPathToNode(string listKey)
		{
			StringCollection path = new StringCollection();

			if (listKey == "")
				return path;

			int id, id2;
			PostCategory pc = null;
			Gallery s = null;
			GalleryPost p = null;
			Group g = null;
			string parentCategories = "";
			string[] ids;
			switch (listKey.Substring(0, 1))
			{
				case "C":
					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					id2 = int.Parse(ids[1].Substring(1));
					parentCategories = ids[2].Substring(1);

					s = Galleries.GetGallery(id2);
					pc = PostCategories.GetCategory(id, s.SectionID);
					g = Galleries.GetGalleryGroup(s.GroupID, true, false);

					break;

				case "S":
					id = int.Parse(listKey.Substring(1));
					s = Galleries.GetGallery(id);
					g = Galleries.GetGalleryGroup(s.GroupID, true, false);

					break;

				case "G":

					id = int.Parse(listKey.Substring(1));
					g = Galleries.GetGalleryGroup(id, true, false);

					break;

				case "P":

					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					parentCategories = ids[2].Substring(1);
					p = GalleryPosts.GetPicture(id);
					s = Galleries.GetGallery(p.SectionID);
					g = Galleries.GetGalleryGroup(s.GroupID, true, false);

					break;

				default:
					return path;
			}

			if (g != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, -1, -1, -1, ""));

			if (s != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, s.SectionID, -1, -1, ""));

			if (!Globals.IsNullorEmpty(parentCategories))
			{
				ids = parentCategories.Split(',');
				parentCategories = "";
				for (int i = 0; i < ids.Length; i++)
				{
					path.Add(BrowseableList.MakeListKey(g.GroupID, s.SectionID, int.Parse(ids[i]), -1, parentCategories));
						
					if (i > 0)
						parentCategories += ",";

					parentCategories += ids[i];
				}
			}

			if (pc != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, s.SectionID, pc.CategoryID, -1, parentCategories));

			if (p != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, s.SectionID, -1, p.PostID, parentCategories));

			return path;
		}

		public override ArrayList GetBrowseableListOptions()
		{
			ArrayList options = new ArrayList();
			
			ArrayList values = new ArrayList();
			values.Add(new BrowseableListItemOptionSelectableValue(CommunityServer.Galleries.Components.GalleryImageType.Thumbnail.ToString(), ResourceManager.GetString( "Gallery_PictureSizes_Thumbnail" )));
			values.Add(new BrowseableListItemOptionSelectableValue(CommunityServer.Galleries.Components.GalleryImageType.SecondaryThumbnail.ToString(), ResourceManager.GetString( "Gallery_PictureSizes_Small" )));
			values.Add(new BrowseableListItemOptionSelectableValue(CommunityServer.Galleries.Components.GalleryImageType.Slideshow.ToString(), ResourceManager.GetString( "Gallery_PictureSizes_Medium" )));
			values.Add(new BrowseableListItemOptionSelectableValue(CommunityServer.Galleries.Components.GalleryImageType.Details.ToString(), ResourceManager.GetString( "Gallery_PictureSizes_Large" )));
			values.Add(new BrowseableListItemOptionSelectableValue(CommunityServer.Galleries.Components.GalleryImageType.Original.ToString(), ResourceManager.GetString( "Gallery_PictureSizes_Original" )));
			options.Add(new BrowseableListItemOption("imageSize", ResourceManager.GetString("Gallery_PictureSizeOption_Name"), BrowseableListItemOptionType.String, values));

			values = new ArrayList();
			values.Add(new BrowseableListItemOptionSelectableValue("Details", ResourceManager.GetString( "Gallery_LinkOption_ImageDetails" )));
			values.Add(new BrowseableListItemOptionSelectableValue("Original", ResourceManager.GetString( "Gallery_LinkOption_OriginalImage" )));
			values.Add(new BrowseableListItemOptionSelectableValue("None", ResourceManager.GetString( "Gallery_LinkOption_None" )));
			options.Add(new BrowseableListItemOption("linkType", ResourceManager.GetString("Gallery_LinkOption_Name"), BrowseableListItemOptionType.String, values));

			return options;
		}

		#endregion
	}

	public class GalleryBrowseableContainer : BrowseableListContainer
	{
		private Group _group;
		private Section _section;
		private PostCategory _category;
		private string _parentCategoryIds;

		public GalleryBrowseableContainer(Group group, Section section, PostCategory category, string parentCategoryIds):base(group,section,category, parentCategoryIds)
		{
			_group = group;
			_section = section;
			_category = category;
			_parentCategoryIds = parentCategoryIds;
		}

		#region IBrowseableListContainer Members

        public override HyperLink AddItemUrl
		{
			get
			{
				if (_section != null)
				{
					HyperLink h = new HyperLink();
					h.Text = ResourceManager.GetString("GalleryNav_AddPicture");
					h.NavigateUrl = GalleryUrls.Instance().ControlPanel_Photos_NewPostModal(_section.SectionID, Globals.UrlEncode(CSContext.Current.RawUrl));
					return h;
				}
				else
					return null;
			}
		}
        
		public override System.Collections.ArrayList Items
		{
			get
			{
				if (_section != null)
				{
					GalleryThreadQuery query = new GalleryThreadQuery();
					query.SectionID = _section.SectionID;
					query.PageIndex = 0;
					query.PageSize = 999999;
					query.ApplicationPostType = GalleryPostType.Image;

				        query.PublishedFilter = GalleryPostPublishedFilter.Published;

					if (_category != null)
						query.CategoryID = _category.CategoryID;

					ArrayList items = new ArrayList();
					ArrayList pictures = Components.GalleryPosts.GetPictures(query).Threads;
					foreach (GalleryPost p in pictures)
					{
						items.Add(new GalleryBrowseableItem(p, _parentCategoryIds));
					}

					return items;
				}
				else
					return null;
			}
		}


		public override System.Collections.ArrayList Containers
		{
			get
			{
				if (_group != null)
				{
					if (_section == null)
					{
						ArrayList containers = new ArrayList();
						ArrayList sections = Galleries.GetGalleriesByGroupID(_group.GroupID, false, true, false);
						foreach (Gallery s in sections)
						{
							if (BrowseableList.UserCanView( s, CSContext.Current.User))
								containers.Add(new GalleryBrowseableContainer(_group, s, null, ""));
						}

						return containers;
					}
					if (_category == null)
					{
						ArrayList containers = new ArrayList();
						ArrayList categories = PostCategories.GetCategories(_section.SectionID, false, 0);
						foreach (PostCategory pc in categories)
						{
							containers.Add(new GalleryBrowseableContainer(_group,_section, pc, ""));
						}

						return containers;
					}
					else
					{
						//get any child categories
						ArrayList containers = new ArrayList();
						ArrayList categories = PostCategories.GetCategories(_section.SectionID, false, _category.CategoryID);
						foreach (PostCategory pc in categories)
						{
							containers.Add(new GalleryBrowseableContainer(_group, _section, pc, (!Globals.IsNullorEmpty(_parentCategoryIds) ? _parentCategoryIds + "," : "") + _category.CategoryID));
						}
						
						return containers;
					}
				}
//				if (_section != null)
//				{
//					{
//						ArrayList containers = new ArrayList();
//						ArrayList categories = PostCategories.GetCategories(_section.SectionID, false, _category.CategoryID);
//						foreach (PostCategory pc in categories)
//						{
//							containers.Add(new GalleryBrowseableContainer(_section, pc));
//						}
//
//						return containers;
//					}
//				}

				return null;
			}
		}

		#endregion
	}

	public class GalleryBrowseableItem : BrowseableListItem
	{
		private GalleryPost _picture;

		public GalleryBrowseableItem(GalleryPost picture, string parentCategoryIds):base(picture, parentCategoryIds)
		{
			_picture = picture;
		}

		#region IBrowseableListItem Members


		public override string GetRenderHTML(NameValueCollection optionValues)
		{
			//imageSize = CommunityServer.Galleries.Components.GalleryImageType
			//linkType = "Details", "Original", "None"
			
			string linkStart;
			string linkEnd;
			string image;

			switch (optionValues["linkType"])
			{
				case "Original":
					linkStart = "<a href=\"" + GalleryUrls.Instance().PictureUrl(_picture) + "\" target=\"_blank\">";
					linkEnd = "</a>";
					break;

				case "None":
					linkStart = "";
					linkEnd = "";
					break;

				case "Details":
				default:
					linkStart = "<a href=\"" + GalleryUrls.Instance().ViewPicture(_picture.Gallery.ApplicationKey, _picture) + "\" target=\"_blank\">";
					linkEnd = "</a>";
					break;
			}

			GalleryImageType imageType = GalleryImageType.Thumbnail;
			if (!Globals.IsNullorEmpty(optionValues["imageSize"]))
				imageType = (GalleryImageType) Enum.Parse(typeof(GalleryImageType), optionValues["imageSize"], true);

			switch (imageType)
			{
				case GalleryImageType.SecondaryThumbnail:
					image = "<img src=\"" + ImageHandling.GetPictureUrl(_picture, _picture.Gallery.GetSecondaryThumbnailSettings()) + "\" border=\"0\">";
					break;

				case GalleryImageType.Slideshow:
					image = "<img src=\"" + ImageHandling.GetPictureUrl(_picture, _picture.Gallery.GetSlideshowSettings()) + "\" border=\"0\">";
					break;

				case GalleryImageType.Details:
					image = "<img src=\"" + ImageHandling.GetPictureUrl(_picture, _picture.Gallery.GetPictureDetailsSettings()) + "\" border=\"0\">";
					break;

				case GalleryImageType.Original:
					image = "<img src=\"" + GalleryUrls.Instance().PictureUrl(_picture) + "\" border=\"0\">";
					break;

				case GalleryImageType.Thumbnail:
				default:
					image = "<img src=\"" + ImageHandling.GetPictureUrl(_picture, _picture.Gallery.GetThumbnailSettings()) + "\" border=\"0\">";
					break;
			}
			
			return linkStart + image + linkEnd;
		}

		public override string PreviewHTML
		{
			get
			{
				return string.Format("<img src=\"{0}\" border=\"0\" />", ImageHandling.GetPictureUrl(_picture, _picture.Gallery.GetThumbnailSettings()));
			}
		}

		#endregion
	}

}

